package com.ss.android.article.browser.view.user;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView.ItemDecoration;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.SimpleItemAnimator;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.URLUtil;
import android.widget.TextView;

import com.a.a.e;
import com.a.a.s;
import com.bytedance.common.utility.UIUtils;
import com.ss.android.article.browser.download.DownloadTaskManager;
import com.ss.android.article.browser.download.internal.DownloadListener;
import com.ss.android.article.browser.download.internal.DownloadState;
import com.ss.android.article.browser.download.internal.DownloadTask;
import com.ss.android.article.browser.download.internal.DownloadOperator;
import com.ss.android.article.browser.view.multiselect.a;
import com.ss.android.article.browser.view.multiselect.f;
import com.ss.android.common.app.AbsFragment;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.CopyOnWriteArraySet;

public final class i extends AbsFragment implements com.ss.android.article.browser.download.aa.a$b, com.ss.android.article.browser.view.multiselect.d, f, t, u {
    private View a;
    private View b;
    private RecyclerView c;
    private View d;
    private com.ss.android.article.browser.view.user.a.d e;
    private a f;
    private v g;

    public i() {

    }

    static void a(i arg3) {
        if (arg3.e != null) {
            DownloadTaskManager v1 = DownloadTaskManager.getInstance();
            Iterator v2 = v1.mDownloadOperatorMap.keySet().iterator();
            while (v2.hasNext()) {
                Object v0 = v2.next();
                if (v0 == null) {
                    continue;
                }

                v1.a(((DownloadTask) v0));
            }

            arg3.f.notifyDataSetChanged();
        }
    }

    static void a(i arg0, boolean arg1) {
        arg0.b(arg1);
    }

    static void a(ArrayList arg3) {
        Iterator v1 = arg3.iterator();
        while (v1.hasNext()) {
            Object v0 = v1.next();
            try {
                DownloadTaskManager.getInstance().d(((DownloadTask) v0));
                DownloadTaskManager.a(((DownloadTask) v0).getFilePath());
            } catch (Exception v0_1) {
            }
        }
    }

    public final void a() {
        if (this.g != null) {
            this.g.b();
        }

        this.g();
    }

    public final void a(int arg15) {
        DownloadTask v0_3;
        int v13 = 3;
        int v12 = 2;
        String v5 = null;
        List v0 = this.e.a;
        if (v0.size() > arg15) {
            Object v8 = v0.get(arg15);
            switch (n.a[((DownloadTask) v8).downloadState.ordinal()]) {
                case 1:
                case 2: {
                    DownloadTaskManager.getInstance().a(((DownloadTask) v8));
                    break;
                }
                case 3:
                case 4: {
                    DownloadTaskManager v11 = DownloadTaskManager.getInstance();
                    if (((DownloadTask) v8).fileDir == null || ((DownloadTask) v8).fileDir.trim().length() == 0) {
                        Log.w("DownloadTaskManager", "file path is invalid. file path : " + ((DownloadTask) v8).fileDir + ", use default file path : " + DownloadTaskManager.a);
                        ((DownloadTask) v8).fileDir = DownloadTaskManager.a;
                    }

                    if (((DownloadTask) v8).fileName != null && ((DownloadTask) v8).fileName.trim().length() != 0) {
                        if (((DownloadTask) v8).url != null && (URLUtil.isNetworkUrl(((DownloadTask) v8).url))) {
                            if (v11.mDownloadListenerMap.get(v8) == null) {
                                v11.mDownloadListenerMap.put((DownloadTask) v8, new CopyOnWriteArraySet());
                            }

                            ((DownloadTask) v8).downloadState = DownloadState.INITIALIZE;
                            String v6 = ((DownloadTask) v8).getFilePath();
                            SQLiteDatabase v0_1 = v11.mDownloadDBHelper.getReadableDatabase();
                            String[] v2 = new String[10];
                            v2[0] = "url";
                            v2[1] = "downloadState";
                            v2[v12] = "file_dir";
                            v2[v13] = "filename";
                            v2[4] = "title";
                            v2[5] = "thumbnail";
                            v2[6] = "finishedSize";
                            v2[7] = "totalSize";
                            v2[8] = "file_path";
                            v2[9] = "create_time";
                            Cursor v6_1 = v0_1.query("download", v2, "file_path=?", new String[]{v6}, v5, v5, v5);
                            if (v6_1 != null) {
                                if (v6_1.moveToNext()) {
                                    v0_3 = new DownloadTask(v6_1.getString(0), v6_1.getString(v12), v6_1.getString(v13), v6_1.getString(4), v6_1.getString(5));
                                    v0_3.downloadState = DownloadState.valueOf(v6_1.getString(1));
                                    v0_3.finishedSize = ((long) v6_1.getInt(6));
                                    v0_3.totalSize = ((long) v6_1.getInt(7));
                                    v6_1.getString(8);
                                    v0_3.createTime = v6_1.getLong(9);
                                } else {
                                    v0_3 = null;
                                }
                                v6_1.close();
                            } else {
                                v0_3 = null;
                            }

                            if (!((DownloadTask) v8).equals(v0_3)) {
                                v11.insertDatabase(((DownloadTask) v8));
                            }

                            DownloadOperator v1 = new DownloadOperator(v11, ((DownloadTask) v8));
                            v11.mDownloadOperatorMap.put((DownloadTask) v8, v1);
                            Iterator<DownloadListener> v2_1 = v11.e(((DownloadTask) v8)).iterator();
                            while (v2_1.hasNext()) {
                                v2_1.next().f(((DownloadTask) v8));
                            }

                            DownloadListener v0_4 = v11.e;
                            if (v0_4 != null) {
                                v0_4.f(((DownloadTask) v8));
                            }

                            v1.startDownload();
                        } else {
                            Log.w("DownloadTaskManager", "invalid http url: " + ((DownloadTask) v8).url);
                            throw new IllegalArgumentException("invalid http url");
                        }
                    } else {
                        Log.w("DownloadTaskManager", "file name is invalid. file name : " + ((DownloadTask) v8).fileName);
                        throw new IllegalArgumentException("file name is invalid");
                    }
                }
                default: {
                    if ((com.ss.android.article.browser.download.b.a.b(((DownloadTask) v8).getFilePath())
                            & com.ss.android.article.browser.download.b.a.a(((DownloadTask) v8).getFilePath()))) {
                        com.ss.android.article.browser.download.b.a.c(((DownloadTask) v8).getFilePath());
                    } else {
                    }

                    break;
                }
            }
            View v1_1 = this.b;
            boolean v0_5 = !DownloadTaskManager.getInstance().c() ? true : false;
            v1_1.setEnabled(v0_5);
        }
    }

    public final void a(DownloadTask arg1) {
        this.g(arg1);
    }

    public final void a(DownloadTask arg1, long arg2, long arg4) {
        arg1.finishedSize = arg2;
        arg1.totalSize = arg4;
        this.g(arg1);
    }

    public final void a(boolean arg2) {
        if (this.f != null) {
            if (arg2) {
                this.f.c();
            } else {
                this.f.e();
            }
        }
    }

    private void b(boolean arg5) {
        int v2 = 8;
        boolean v1 = false;
        View v3 = this.d;
        int v0 = arg5 ? 0 : v2;
        UIUtils.setVisibility(v3, v0);
        View v0_1 = this.b;
        if (!arg5) {
            v2 = 0;
        }

        UIUtils.setVisibility(v0_1, v2);
        if (this.g != null) {
            v v0_2 = this.g;
            if (!arg5) {
                v1 = true;
            }

            v0_2.b(v1);
        }
    }

    static View b(i arg1) {
        return arg1.b;
    }

    public final void b() {
        if (this.g != null) {
            this.g.c();
        }
    }

    public final void b(int arg2) {
        if (arg2 >= this.e.getItemCount() && this.g != null) {
            this.g.a();
        }

        this.g();
    }

    public final void b(DownloadTask arg4) {
        if (arg4 != null) {
            if (this.e.a.contains(arg4)) {
                int v0 = this.e.a(arg4);
                this.e.a.remove(v0);
                this.f.notifyItemRangeRemoved(v0, 1);
            }

            this.f.notifyItemChanged(this.e.a(arg4));
            this.b(this.e.a.isEmpty());
        }
    }

    static com.ss.android.article.browser.view.user.a.d c(i arg1) {
        return arg1.e;
    }

    public final void c() {
        this.f.a();
        this.g();
    }

    public final void c(DownloadTask arg1) {
        this.g(arg1);
    }

    static a d(i arg1) {
        return arg1.f;
    }

    public final void d() {
        s v0 = s.a(new m(this));
        ((e) v0).b = android.support.design.a.c();
        ((e) v0).c = android.support.design.a.b();
        v0.a(new l(this));
    }

    public final void d(DownloadTask arg1) {
        this.g(arg1);
    }

    static v e(i arg1) {
        return arg1.g;
    }

    public final void e() {
        this.f.b();
        this.g();
    }

    public final void e(DownloadTask arg1) {
        this.g(arg1);
    }

    public final void f() {
        if (this.g != null) {
            v v1 = this.g;
            boolean v0 = this.d.getVisibility() != View.VISIBLE ? true : false;
            v1.b(v0);
        }
    }

    public final void f(DownloadTask arg1) {
        this.g(arg1);
    }

    private void g() {
        if (this.g != null) {
            v v1 = this.g;
            boolean v0 = this.f.f().size() > 0 ? true : false;
            v1.a(v0);
        }
    }

    private void g(DownloadTask arg3) {
        if (arg3 != null && this.e != null) {
            List v0 = this.e.a;
            if (v0.contains(arg3)) {
                this.f.notifyItemChanged(v0.indexOf(arg3));
            }
        }
    }

    public final void onActivityCreated(@Nullable Bundle arg3) {
        super.onActivityCreated(arg3);
        s v0 = DownloadTaskManager.getInstance().b();
        ((e) v0).b = android.support.design.a.c();
        ((e) v0).c = android.support.design.a.b();
        v0.a(new k(this));
    }

    public final void onAttach(Context arg2) {
        super.onAttach(arg2);
        if ((arg2 instanceof v)) {
            this.g = ((v) arg2);
        }
    }

    public final void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        FragmentActivity v0 = this.getActivity();
        if ((v0 instanceof UserActionHistoryActivity)) {
            com.ss.android.article.browser.download.aa v0_1 = ((UserActionHistoryActivity) v0).a;
            if (v0_1 != null) {
                v0_1.a = this;
            }
        }
    }

    @Nullable
    public final View onCreateView(LayoutInflater arg6, @Nullable ViewGroup arg7, @Nullable Bundle arg8) {
        this.a = arg6.inflate(com.ss.android.article.browser.R.layout.du, arg7, false);
        this.b = this.a.findViewById(com.ss.android.article.browser.R.id.vf);
        this.c = this.a.findViewById(com.ss.android.article.browser.R.id.recycler_view);
        this.d = this.a.findViewById(com.ss.android.article.browser.R.id.s9);
        ((TextView) this.d.findViewById(com.ss.android.article.browser.R.id.s_)).setText(com.ss.android.article.browser.R.string.jj);
        com.bytedance.frameworks.a.d.a v0 = new com.bytedance.frameworks.a.d.a(this.getContext());
        v0.a = 1;
        v0.a(this.getResources().getDrawable(com.ss.android.article.browser.R.color.fv));
        this.c.addItemDecoration(((ItemDecoration) v0));
        ((SimpleItemAnimator) this.c.getItemAnimator()).setSupportsChangeAnimations(false);
        this.e = new com.ss.android.article.browser.view.user.a.d();
        this.f = com.ss.android.article.browser.view.multiselect.c.a(this.getActivity()).a(this.e).a(this.e.a).a(
                new com.ss.android.article.browser.view.multiselect.viewholder.a.a((byte) 0))
                .a(((f) this)).a(((com.ss.android.article.browser.view.multiselect.d) this)).a();
        this.c.setAdapter(this.f);
        this.c.setLayoutManager(new LinearLayoutManager(this.getContext()));
        View v3 = this.b;
        boolean v0_1 = !DownloadTaskManager.getInstance().c() ? true : false;
        v3.setEnabled(v0_1);
        this.b.setOnClickListener(new j(this));
        return this.a;
    }

    public final void onDestroy() {
        super.onDestroy();
        FragmentActivity v0 = this.getActivity();
        if ((v0 instanceof UserActionHistoryActivity)) {
            com.ss.android.article.browser.download.aa v0_1 = ((UserActionHistoryActivity) v0).a;
            if (v0_1 != null) {
                v0_1.a = null;
            }
        }
    }

    public final void onDetach() {
        super.onDetach();
        this.g = null;
    }
}

